<template>
  <div class="security-page">
    <el-card>
    <div class="header-title">保安管理</div>
    <div class="security-header">
      <el-input v-model="queryParams.securityName" placeholder="保安姓名" class="search-input" clearable />
      <el-input v-model="queryParams.contactPhone" placeholder="联系电话" class="search-input" clearable />
      <el-button type="primary" @click="handleQuery">查询</el-button>
      <el-button @click="resetQuery">重置</el-button>
      <el-button type="primary" class="add-btn" @click="handleAdd">+ 新增保安</el-button>
    </div>
    <el-alert :title="`查询结果：共计 ${total} 条数据`" type="info" show-icon class="result-alert" />
    <el-table :data="securityList" border stripe class="security-table" v-loading="loading">
      <el-table-column prop="securityName" label="保安姓名" width="120" />
      <el-table-column prop="contactPhone" label="联系电话" width="140" />
      <el-table-column prop="updateTime" label="更新时间" width="180" />
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <el-link type="primary" @click="handleEdit(scope.row)">编辑</el-link>
          <el-link type="danger" @click="handleDelete(scope.row)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        v-model:current-page="queryParams.current"
        v-model:page-size="queryParams.size"
        :page-sizes="[10, 20, 50, 100]"
        :total="total"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getSecurityPage, deleteSecurity } from '@/api/security'
import type { SysSecurity } from '@/types/security'

const router = useRouter()
const loading = ref(false)
const total = ref(0)
const securityList = ref<SysSecurity[]>([])

const queryParams = reactive({
  securityName: '',
  contactPhone: '',
  current: 1,
  size: 10
})

const getList = async () => {
  loading.value = true
  try {
    const res = await getSecurityPage(queryParams)
    console.log(res)
    securityList.value = res.data.records
    total.value = res.data.total
  } finally {
    loading.value = false
  }
}

const handleQuery = () => {
  queryParams.current = 1
  getList()
}

const resetQuery = () => {
  queryParams.securityName = ''
  queryParams.contactPhone = ''
  handleQuery()
}

const handleSizeChange = (val: number) => {
  queryParams.size = val
  getList()
}

const handleCurrentChange = (val: number) => {
  queryParams.current = val
  getList()
}

const handleAdd = () => {
  router.push('/main/security/add')
}

const handleEdit = (row: SysSecurity) => {
  router.push(`/main/security/edit/${row.id}`)
}

const handleDelete = (row: SysSecurity) => {
  ElMessageBox.confirm('确认删除该保安信息吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    await deleteSecurity(row.id!)
    ElMessage.success('删除成功')
    getList()
  })
}

onMounted(() => {
  getList()
})
</script>

<style scoped>
.header-title {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 16px;
}
.security-page {
  padding: 24px;
  background: #f7f8fa;
  min-height: 100vh;
}
.security-header {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
}
.search-input {
  width: 200px;
  margin-right: 8px;
}
.add-btn {
  margin-left: auto;
}
.result-alert {
  margin-bottom: 16px;
}
.security-table {
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
}
.el-link + .el-link {
  margin-left: 8px;
}
.pagination {
  margin-top: 16px;
  display: flex;
  justify-content: flex-end;
}
</style>
